{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "numeric-luxury",
   "metadata": {},
   "source": [
    "# Differentially Private EBMs\n",
    "\n",
    "Links to API References: [DPExplainableBoostingClassifier](./python/api/DPExplainableBoostingClassifier.ipynb), [DPExplainableBoostingRegressor](./python/api/DPExplainableBoostingRegressor.ipynb)\n",
    "\n",
    "*See the reference paper for full details [[1](dp_ebms)].*  [Link](https://proceedings.mlr.press/v139/nori21a/nori21a.pdf)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "announced-warning",
   "metadata": {},
   "source": [
    "<h2>Code Example</h2>\n",
    "\n",
    "The following code will train a DPEBM classifier for the adult income dataset. The visualizations provided will be for both global and local explanations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bibliographic-mouse",
   "metadata": {},
   "outputs": [],
   "source": [
    "from interpret import set_visualize_provider\n",
    "from interpret.provider import InlineProvider\n",
    "set_visualize_provider(InlineProvider())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "packed-excess",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import roc_auc_score\n",
    "\n",
    "from interpret.privacy import DPExplainableBoostingClassifier\n",
    "from interpret import show\n",
    "\n",
    "df = pd.read_csv(\n",
    "    \"https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data\",\n",
    "    header=None)\n",
    "df.columns = [\n",
    "    \"Age\", \"WorkClass\", \"fnlwgt\", \"Education\", \"EducationNum\",\n",
    "    \"MaritalStatus\", \"Occupation\", \"Relationship\", \"Race\", \"Gender\",\n",
    "    \"CapitalGain\", \"CapitalLoss\", \"HoursPerWeek\", \"NativeCountry\", \"Income\"\n",
    "]\n",
    "X = df.iloc[:, :-1]\n",
    "y = df.iloc[:, -1]\n",
    "\n",
    "feature_types = ['continuous', 'nominal', 'continuous', 'nominal',\n",
    "    'continuous', 'nominal', 'nominal', 'nominal', 'nominal', 'nominal',\n",
    "    'continuous', 'continuous', 'continuous', 'nominal']\n",
    "\n",
    "privacy_bounds = {\"Age\": (17, 90), \"fnlwgt\": (12285, 1484705), \n",
    "    \"EducationNum\": (1, 16), \"CapitalGain\": (0, 99999), \n",
    "    \"CapitalLoss\": (0, 4356), \"HoursPerWeek\": (1, 99)\n",
    "}\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20)\n",
    "\n",
    "dpebm = DPExplainableBoostingClassifier(random_state=None, epsilon=1.0, delta=1e-5, \n",
    "    feature_types=feature_types, privacy_bounds=privacy_bounds)\n",
    "dpebm.fit(X_train, y_train)\n",
    "\n",
    "auc = roc_auc_score(y_test, dpebm.predict_proba(X_test)[:, 1])\n",
    "print(\"AUC: {:.3f}\".format(auc))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3819596",
   "metadata": {},
   "outputs": [],
   "source": [
    "show(dpebm.explain_global())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "asdfasdfdsa",
   "metadata": {},
   "source": [
    "<br/>\n",
    "<br/>\n",
    "<br/>\n",
    "<br/>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "kgkwkjweks",
   "metadata": {},
   "outputs": [],
   "source": [
    "show(dpebm.explain_local(X_test[:5], y_test[:5]), 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bed7cb88",
   "metadata": {},
   "source": [
    "<br/>\n",
    "<br/>\n",
    "<br/>\n",
    "<br/>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "engaging-string",
   "metadata": {},
   "source": [
    "<h2>Bibliography</h2>\n",
    "\n",
    "(dp_ebms)=\n",
    "[1] Harsha Nori, Rich Caruana, Zhiqi Bu, Judy Hanwen Shen, and Janardhan Kulkarni. Accuracy, Interpretability, and Differential Privacy via Explainable Boosting. In Proceedings of the 38th International Conference on Machine Learning, 8227-8237. 2021. [Paper Link](https://proceedings.mlr.press/v139/nori21a/nori21a.pdf)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
